package Servlet;

import Bean.DgutBean;
import loginRegister.LoginBean;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.ResultSet;

import Bean.SqlBean;
import lookMessage.LookMessageBean;

/**
 * @author 黄忠
 * 创建于2019-04-09
 */
@WebServlet(urlPatterns = {"/login/unbind"})
public class UnBindServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) throws IOException {

        HttpSession session = request.getSession(true);
        LoginBean loginBean=(LoginBean) session.getAttribute("login");
        if(loginBean!=null) {
            String userName = loginBean.getUserName();
            DgutBean dgutBean = new DgutBean(userName);
            dgutBean.unBind();
            try {
                ResultSet rs = null;
                SqlBean sb = new SqlBean();
                sb.connectSQL();
                String sql2 = "select * from user where userName='" + userName + "'";
                rs = sb.executeQuery(sql2);
                LookMessageBean mm = new LookMessageBean();
                while (rs.next()) {
                    mm.setName(rs.getString("name"));
                    mm.setSex(rs.getString("sex"));
                    mm.setIdCard(rs.getString("idCard"));
                    mm.setBirth(rs.getString("birth"));
                    mm.setNation(rs.getString("nation"));
                    mm.setEdu(rs.getString("edu"));
                    mm.setWork(rs.getString("work"));
                    mm.setMonthPay(rs.getString("monthPay"));
                    mm.setPhone(rs.getString("phone"));
                    mm.setPlace(rs.getString("place"));
                    mm.setEmail(rs.getString("email"));
                    mm.setLevel(rs.getString("level"));
                    mm.setPicture(rs.getString("picture"));
                    mm.setDgut_username(rs.getString("dgut_username"));
                }
                session.setAttribute("wordlist", mm);
                sb.closeSQL();
                response.sendRedirect("https://cas.dgut.edu.cn/logout?callback=http://localhost:8080/main/main.jsp");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        else
        {
            response.sendRedirect("http://localhost:8080/login.jsp");
        }

    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doGet(request,response);
    }
}